package com.gantang.poc.mock;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.io.InputStream;

public class BaseMockData {
    private static ObjectMapper objectMapper;

    static {
        objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    // 泛型方法，使用指定的JSON文件和目标类型来解析和返回数据对象
    public static <T> T mockData(String fileName, Class<T> type) {
        try (InputStream inputStream = MockData.class.getClassLoader().getResourceAsStream(fileName)) {
            if (inputStream == null) {
                throw new RuntimeException("加载\t" + fileName + "\t文件失败: 文件未找到");
            }
            return objectMapper.readValue(inputStream, type);
        } catch (IOException e) {
            throw new RuntimeException("加载\t" + fileName + "\t文件失败: 读取错误", e);
        }
    }
}
